<?php

namespace app\index\model;

use  think\Db;

class PurchaseplanfollowModel
 {
    private $paramObj='';
     //用户列表
     static public function getList($param, $titles = null, $function = null)
     {
        $where=[];
        if (!empty($param['sku'])) {
             $where['sku']=$param['sku'];
         }
         if (!empty($param['code'])) {
             $where['purchase_plan_sn']=$param['code'];
         }
         $sql=Db::name('purchase_plan')->field('*')->where($where);
        return \app\common\service\ToolService::downOrReturn($sql, $param, $titles,$function);
     }
     static public function getPercentage(){
        $denominator=[];
        $purchaseSql=Db::table('purchase_plan')->field('po,concat(po,"_",sku) as po_sku,sum(quantity) totalquantity')->group('po_sku')->buildSql();
        $purchase=Db::table("$purchaseSql")->alias('p')->join('hz_purchaseorder_details d' ,'concat(purchaseorder_id,"_",sku) = p.po_sku','left')
                ->join('hz_purchaseorders o' ,'o.id = p.po','left')    
                ->field('po,po_sku,totalquantity,quantity as purchase_quantity,purchaseorder_date')->select();
        if($purchase){
            foreach($purchase as $key=>$value){
                if(empty($value['po'])){
                    continue;
                }
                $denominator[$value['po_sku']]=$value;
                $denominator[$value['po_sku']]['percentage']=bcdiv($value['purchase_quantity'],$value['totalquantity'],2);
            }
        }
        return $denominator;
     }
 }

